# -*- coding: utf-8 -*-
# @Time    : 2020/11/8 19:02
# @Author  : DuJiabao
# @File    : graph.py
# @Description : 
"""
参考：极客时间 https://time.geekbang.org/column/article/70891?utm_campaign=guanwang&utm_source=baidu-ad&utm_medium=ppzq-pc&utm_content=title&utm_term=baidu-ad-ppzq-title
"""


class Node(object):
    def __init__(self, name):
        self.name = name
        self.next = None


class Graph(object):
    """
    这是一个基于邻接表的无向图
    """

    def __init__(self):
        self.LinkList = {}
        self.num = 0

    def __len__(self):
        return self.num

    def addUser(self, name):
        if name not in self.LinkList:
            self.LinkList[name] = Node(None)
            self.num += 1
            print(f"Add one user '{name}'")
        else:
            print("There is a name conflict! Please rename you nick name!")

    def addEdge(self, user1, user2):
        if user1 not in self.LinkList:
            self.addUser(user1)
            self.num += 1
        if user2 not in self.LinkList:
            self.addUser(user2)
            self.num += 1
        point = self.LinkList[user1]
        while point.next:
            point = point.next
        point.next = user2

        point = self.LinkList[user2]
        while point.next:
            point = point.next
        point.next = user1


if __name__ == '__main__':
    wechat = Graph()
    wechat.addUser("John")
    wechat.addUser("Amy")
    wechat.addEdge("John", "Amy")


